﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Diagnostics;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using OpenTK.Graphics;
using System.ComponentModel;
using System.Data;

using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using OpenTK.Math;


using System.Threading;
using System.Xml;

using System.Xml.XPath;
using System.Windows.Forms;

using XText = Xeno.Core.XenoText.XenoTextCache;


namespace Xeno.Core
{
    public class XenoText
    {
        public class XenoTextCache
        {
            public XenoTexture2D Visual { get; set; }
            public int Width { get; set; }
            public int Height { get; set; }
            public XenoTextCache(Bitmap bitmap,int width,int height)
            {
                Visual = new XenoTexture2D(bitmap);
                Width = width;
                Height = height;
            }

        }

        public static Font CurrentFont { get; set; }

        static Bitmap TmpMap = null;

        static XenoMaterial TextMat = null;
        static XenoEffect TextEffect = null;
        public static void Init()
        {
            SetFont(FontFamily.GenericSansSerif,10);
            TmpMap = new Bitmap(512, 512);
            TextMat = new XenoMaterial("DefaultTextMaterial");
            TextEffect = new XenoEffect("", "Content/Shader/XenoMaterial_Text.fragment");
            TextMat.Effect = TextEffect;
        }


        public static void SetFont(FontFamily family,int size)
        {
            CurrentFont = new Font(family, size);

        }

        public static XenoTextCache CreateText(string text)
        {

            Graphics tg = Graphics.FromImage(TmpMap);
            SizeF siz = tg.MeasureString(text, CurrentFont);




            Bitmap bitmap = new Bitmap((int)siz.Width,(int)siz.Height);
            Graphics g = Graphics.FromImage(bitmap);

            
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            g.Clear(Color.Transparent);

            TextRenderer.DrawText(g, text,CurrentFont, new Point(0, 0), Color.White);

            // assign the bitmap to a globally defined one
            XenoTextCache cache = new XenoTextCache(bitmap,(int)siz.Width,(int)siz.Height);
            tg.Dispose();

            g.Dispose();
            return cache;
        }

        public static void DrawText(XenoTextCache cache, int x, int y,Color color)
        {
         
            GL.PushAttrib(AttribMask.AllAttribBits);

            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();

            Matrix4 projectionMatrix = Matrix4.CreateOrthographicOffCenter(0,XenoCore.DisplayWidth, XenoCore.DisplayHeight,0, 0, 2);

            GL.LoadMatrix(ref projectionMatrix);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();


            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            GL.Disable(EnableCap.AlphaTest);
            GL.Disable(EnableCap.ScissorTest);

            GL.Color4(color);

            TextMat.DiffuseMap = cache.Visual;

            TextMat.DiffuseColor = new Vector4((float)color.R / 255.0f, (float)color.G / 255.0f, (float)color.B / 255.0f, (float)color.A / 255.0f);

            TextMat.Bind();

            GL.Begin(BeginMode.Quads);

            GL.TexCoord2(0, 0);
            GL.Vertex2(x, y);
            GL.TexCoord2(1, 0);
            GL.Vertex2(x + cache.Width, y);
            GL.TexCoord2(1, 1);
            GL.Vertex2(x + cache.Width, y + cache.Height);
            GL.TexCoord2(0, 1);
            GL.Vertex2(x, y + cache.Height);
            
            GL.End();

            TextMat.Unbind();


            GL.PopAttrib();
        }

    }
}
